<template>
  <div class="app-container">
    <el-form :rules="rules" ref="form" :model="tempUserInfo" label-width="120px">
      <el-form-item label="Account">
        <el-input v-model="tempUserInfo.user_account" disabled></el-input>
      </el-form-item>
      <el-form-item label="Name" prop='user_name'>
        <el-input v-model="tempUserInfo.user_name"></el-input>
      </el-form-item>
      <el-form-item label="Title" prop='title'>
        <el-input v-model="tempUserInfo.title"></el-input>
      </el-form-item>
      <el-form-item label="Email" prop='email'>
        <el-input v-model="tempUserInfo.email"></el-input>
      </el-form-item>
      <el-form-item label="Telephone" prop='telephone'>
        <el-input v-model="tempUserInfo.telephone"></el-input>
      </el-form-item>
      <el-form-item label="birthday">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="Pick a date" v-model="tempUserInfo.birthday" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="sex" prop='sex'>
        <el-radio-group v-model="tempUserInfo.sex">
          <el-radio label="male" value="male"></el-radio>
          <el-radio label="female" value="female"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="User Remark">
        <el-input type="textarea" v-model="tempUserInfo.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">Commit</el-button>
        <el-button @click="onCancel">Cancel</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { getInfo, updateUserInfo } from '@/api/user'

export default {
  data() {
    return {
      tempUserInfo: {
        user_name: null,
        user_account: null,
        user_pwd: null,
        sex: null,
        title: null,
        email: null,
        telephone: null,
        birthday: null,
        remark: null
      },
      rules: {
        user_name: [{ required: true, message: 'user name is required', trigger: 'change' }],
        sex: [{ required: true, message: 'sex is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }],
        email: [{ required: true, pattern: '^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$', message: 'email is invalid, please reenter it', trigger: 'blur' }],
        telephone: [{ required: true, pattern: '^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$', message: 'telephone is invalid, please reenter it', trigger: 'blur' }]
      }
    }
  },
  created() {
    // this.fetchData()
    this.getInfo()
  },
  methods: {
    // fetchData() {
    //   this.listLoading = true
    //   getServer().then(response => {
    //     this.list = response
    //     this.listLoading = false
    //   })
    // },
    getInfo() {
      getInfo().then(response => {
        this.tempUserInfo = response.user
      })
    },
    onSubmit() {
      updateUserInfo(this.tempUserInfo).then(() => {
        this.$notify({
          title: 'success',
          message: 'Modify Success!',
          type: 'success',
          duration: 2000
        })
      })
    },
    onCancel() {
      this.$refs['form'].resetFields()
      this.getInfo()
      this.$notify({
        title: 'Reset',
        message: 'Reset Success',
        type: 'success',
        duration: 2000
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

